package com.bayern.util;

import java.util.Arrays;

/**
 * 
 * @author linling
 *
 */
public class IPUtils {
	
	/**
	 * 根据ip端开始ip，和子网掩码长度（如23表示255.255.127.0），获取ip端结束ip
	 * @param ipStart
	 * @param mask
	 * @return
	 */
	public static String getIpEnd(String ipStart, int mask) {
		if(ipStart == null || "".equals(ipStart) || mask < 0) {
			return null;
		}
		StringBuilder ipEnd = new StringBuilder();
		int quotient = mask / 8;
		int remainder = mask % 8;
		String[] ipArray = ipStart.split("\\.");
		for(int i = 0; i < quotient; i++) {
			ipEnd.append(ipArray[i]).append(".");
		}
		if(quotient == 4) {
			return ipEnd.substring(0, ipEnd.length()-1);
		}
		int num = 4 - quotient;
		if(remainder != 0) {
			byte[] ip_binary_array = convertIpUniteToByteArray(Integer.valueOf(ipArray[quotient]));
			byte[] byteArray = new byte[8];
			for(int j = 0; j < remainder; j++) {
				byteArray[j] = ip_binary_array[j];
			}
			for(int j = 8; j > remainder; j--) {
				byteArray[j-1] = (byte)1;
			}
			int ipEnd_quotient = convertByteArrayToIpUnite(byteArray);
			ipEnd.append(ipEnd_quotient).append(".");
			
		}
		if(remainder != 0 && num > 1) {
			for(int j = 0; j < num-1; j++) {
				ipEnd.append("255").append(".");
			}
		}else if(remainder == 0 && num > 0){
			for(int j = 0; j < num; j++) {
				ipEnd.append("255").append(".");
			}
		}
		return ipEnd.substring(0, ipEnd.length()-1);
	}
	
	/**
	 * 将ip位转换为8字节无符号byte数组
	 * @param number
	 * @return
	 */
	public static byte[] convertIpUniteToByteArray(int number) {
		if(number < 0 || number > 255) {
			return null;
		}
		byte[] byteArray = new byte[8];
		Arrays.fill(byteArray, (byte)0);
		int index = 7;
		int quotient = number / 2;
		int remainder = number % 2;
		while(number != 0){
			byteArray[index--] = (byte)remainder;
			number = quotient;
			quotient = number / 2;
			remainder = number % 2;
		}
		return byteArray;
	}
	
	/**
	 * 将无符号byte数组转换成ip位 
	 * @param byteArray
	 * @return
	 */
	public static Integer convertByteArrayToIpUnite(byte[] byteArray) {
		if(byteArray == null) {
			return null;
		}
		Integer num = 0;
		for(int i = 0; i < byteArray.length; i++) {
			num += byteArray[i] * (int)(Math.pow(2, (8-i-1)));
		}
		return num;
	}
	
	/**
	 * 将ip字符串转换成long类型（二进制方式）
	 * @param ipStr
	 * @return
	 */
	public static Long convertIpToLong(String ipStr) {
		if(ipStr == null || "".equals(ipStr)){
			return null;
		}
		Long ipLong = 0L;
		String[] ipArray = ipStr.split("\\.");
		if(ipArray.length != 4) {
			return null;
		}
		for(int i = 0; i < ipArray.length; i++) {
			byte[] ipByteArray = convertIpUniteToByteArray(Integer.valueOf(ipArray[i]));
			for(int j = 0; j < ipByteArray.length; j++) {
				ipLong += (long)ipByteArray[j] * (long)Math.pow(2, (((4-i)*8) - j - 1));
			}
		}
		return ipLong;
	}
	
	public static void main(String[] args) {
		String ipEnd = getIpEnd("1.0.2.0", 23);
		System.out.println(ipEnd);
		Long ipLong = convertIpToLong(ipEnd);
		System.out.println(ipLong);
	}
}
